<?php if (!defined('THINK_PATH')) exit();?><div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'center',border:false">
        <!-- Begin of toolbar -->
        <div id="blog-toolbar">
            <div class="wu-toolbar-button">
                <a href="#" class="easyui-linkbutton" iconCls="icon-reload" onclick="searchBlogGrid()" plain="true">刷新</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-back" onclick="removeTab()" plain="true">返回</a>
            </div>
            <div class="wu-toolbar-search">
                <label>微博类型：</label>
                <select class="easyui-combobox" name="type" editable="false" style="width:160px;"
                        panelHeight="auto">
                    <option value="0">全部</option>
                    <option value="1">原创</option>
                    <option value="2">转发</option>
                </select>
                <label>微博内容：</label><input class="easyui-textbox" type="text" name="blog_content"
                                           data-options="prompt:'微博内容'" style="width:240px;"></input>
                <label>微博作者：</label><input class="easyui-textbox" type="text" name="blog_username"
                                           data-options="prompt:'微博作者'" style="width:240px"></input>
                <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="searchBlogGrid()">开始检索</a>
            </div>
        </div>
        <!-- End of toolbar -->
        <table id="blog-datagrid" toolbar="#blog-toolbar" class="easyui-datagrid">
        </table>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $('#blog-datagrid').datagrid({
            url: "<?php echo U('Weibo/getBlogListByPage','','');?>",
            rownumbers: true,
            pagination: true,
            fitColumns: true,
            fit: true,
            singleSelect: true,
            pageSize: 10,
            pageList: [5, 20, 50],
            remoteSort: true,
            multiSort: true,
            columns: [[
                {field: 'id', title: 'ID', align: 'center', width: 40},
                {field: 'uid', title: 'ID', align: 'center', width: 40,hidden:'true'},
                {field:'username',title: '作者',width:80,align:'center'},
                {field:'content',title: '微博内容',width:420,align:'center'},
                {field:'pic',title: '配图',width:80,align:'center',formatter:showPicUrl},
                {field:'turn',title: '转发次数',width:80,align:'center'},
                {field:'keep',title: '收藏次数',width:80,align:'center'},
                {field:'comment',title: '评论次数',width:80,align:'center'},
                {field:'time',title: '发布时间',width:240,align:'center',formatter: format_blogTime},
                {field: 'operation', title: '操作', width: 100, align: 'center', formatter: showBlogBtn}
            ]],
            queryParams: {
                username: "",
                content: "",
                type: 0
            },
            onLoadSuccess: function (data) {
                $("a[name='delBlog']").linkbutton({plain: true, iconCls: 'icon-remove'});
            }
        });
    });

    //格式化图片超级链接
    function showPicUrl(value, row, index) {
        if(value != '' && value != null){
            return "<a href='/MicroBlog/"+value+ "' target='_blank'>显示配图</a>";
        }else{
            return "暂无配图";
        }
    }

    //格式化日期列
    function format_blogTime(value, row, index) {
        return toDateString(value * 1000);
    }

    //显示操作按钮
    function showBlogBtn(value, row, index) {
        return "<a href='#' onclick='removeBlog("+index+")' class='easyui-linkbutton' name='delBlog'>删除</a>";
    }

    //按条件查询表格数据
    function searchBlogGrid() {
        var queryParams = $('#blog-datagrid').datagrid('options').queryParams;
        queryParams.username = $.trim($('input[name=blog_username]').val());
        queryParams.content = $.trim($('input[name=blog_content]').val());
        queryParams.type = $("input[name=type]").val();
        $('#blog-datagrid').datagrid('reload');
    }

    //删除微博
    function removeBlog(rowIndex){
        //获取当前选中行的索引
        selectedRowIndex = $('#blog-datagrid').datagrid('getRowIndex',$('#blog-datagrid').datagrid('getSelected'));
        //判断当前按钮所在行与当前选中行是否一致
        if (rowIndex == selectedRowIndex){
            row = $('#blog-datagrid').datagrid('getSelected');
            if (row){
                delmsg = '你确认要删除当前<b><font color="blue">' + row['username'] + '</font></b>的这条微博？';
                $.messager.confirm('删除微博', delmsg, function(r){
                    if (r){
                        $.ajax({
                            url: "<?php echo U('Weibo/delWeibo','','');?>",
                            type: "post",
                            dataType: "json",
                            data: 'id=' + row['id'] + '&uid=' + row['uid'],
                            success: function (result) {
                                $.messager.show({
                                    title:'系统提示',
                                    msg:result.msg
                                });
                                if (result.errorCode == 0){
                                    // $('#blog-datagrid').datagrid('deleteRow',rowIndex); //根据索引删除当前行
                                    $('#blog-datagrid').datagrid('reload');
                                }
                            }
                        });
                    }
                });
            }
        }
    }

</script>